package com.yuyou.activity.group.job;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.yuyou.activity.group.job.Async.AsyncService;
import com.yuyou.activity.group.service.entity.ActivityGroupJob;
import com.yuyou.activity.group.service.entity.ActivityGroupUser;
import com.yuyou.activity.group.service.service.IActivityGroupJobService;
import com.yuyou.activity.group.service.service.IActivityGroupService;
import com.yuyou.activity.group.service.service.IActivityGroupUserService;
import com.yuyou.activity.util.enums.ActivityGroupJobEnums;
import com.yuyou.activity.util.util.Sequence;

@Component
public class GroupSchedule {

    @Autowired
    private IActivityGroupUserService activityGroupUserService;

    @Autowired
    private IActivityGroupService activityGroupService;

    @Autowired
    private IActivityGroupJobService activityGroupJobService;

    @Autowired
    private AsyncService asyncService;

    /**
     * 更新活动状态
     * 每隔50秒执行任务
     */
    /*@Scheduled(cron="0/50 * * * * ?")
    public void updateActivityStatus(){
        activityGroupService.updateFailActivity();
    }*/

    /**
     * 更新用户状态
     * 每隔60秒执行任务
     */
    /*@Scheduled(cron="0 0/1 * * * ?")
    public void updateUserStatus(){
        //状态为初始化且活动为已成功的用户
        List<ActivityGroupUser> list = activityGroupUserService.findNotUpdateUser();
        List<ActivityGroupJob> jobs = new ArrayList<>();
        Date now = new Date();
        list.forEach(activityGroupUser -> {
            ActivityGroupJob job = new ActivityGroupJob();
            job.setId(Sequence.getSequence().nextId());
            job.setfId(activityGroupUser.getActivityGroupUserId());
            job.setCreateTime(now);
            job.setType(ActivityGroupJobEnums.type.GROUP.getValue());
            job.setStatus(ActivityGroupJobEnums.status.init.getValue());
            job.setTag(ActivityGroupJobEnums.tag.GROUP.getValue());

            jobs.add(job);
        });

        activityGroupJobService.insertJob(list , jobs);

    }*/

    /**
     * 处理job
     * 每隔70秒执行任务
     * @throws Exception
     */
    @Scheduled(cron="0/70 * * * * ?")
    public void jobConsumers() throws Exception {

        //本批次的分派标识
        Long allotTempId=Sequence.getSequence().nextId();
        activityGroupJobService.updateLoadedJob(allotTempId);

        //获取需要处理的任务
        List<ActivityGroupJob> jobs = activityGroupJobService.findLoadedJob(allotTempId);

        //放入线程池
        jobs.forEach(job -> {
            if(ActivityGroupJobEnums.type.GROUP.getValue() == job.getType()){//发送短信给用户
                asyncService.AsyncSendMessageJob(job);
            }else if(ActivityGroupJobEnums.type.SHIPIN.getValue() == job.getType()){//再次同步失败且需要重试的用户订单数据至shipin.com
                asyncService.AsyncShiPinComJob(job);
            }else if(ActivityGroupJobEnums.type.GROUP_SHIPIN.getValue() == job.getType()){//shipin.com团购成功发送短信
                asyncService.AsyncSHIPINJob(job);
            }
        });


    }
}
